<?php


namespace app\index\model;


use app\index\controller\BaseController;
use think\Model;

class ActionRole extends Model
{
    public static function checkUserActionRole($action)
    {
        $user_session=(new BaseController())->user_session;
        $UserClass=new User();

        if(!is_array($action)){
            $action=[$action];
        }

        $role=$UserClass->alias('u')
            ->join('action_role ar','u.id=ar.user_id or u.role=ar.role_id or u.role_detail_id=ar.role_detail_id or u.group_id=ar.group_id','left')
            ->join('__ACTION__ a','ar.action_id=a.id','left')
            ->where('u.id',$user_session['id'])
            ->where('a.action_name','in',$action)
            ->where('u.status',1)
            ->where('u.del_flag',0)
            ->where('ar.del_flag',0)
            ->where('a.del_flag',0)
            ->field('a.action_name,max(ar.action_type) action_type')
            ->group('a.action_name')
            ->select();
        $action_arr=$role->toArray();
        if(empty($action_arr)){
            exit(json_encode(['code'=>1,'msg'=>'Permission denied']));
        }
        $action_type=0;
        $action_type_list=[];
        foreach ($action_arr as $key=>$value){
            if($value['action_name']===$action[0]){
                $action_type=$value['action_type'];
            }
            $action_type_list[$value['action_name']]=$value['action_type'];
        }

        if($action_type===1) {
//            $search_user = User::getSearchUser($user_session['group_id']);
            $search_user = null;
            $group=$user_session['group_id'];
        }else if($action_type===2){
            $search_user=null;
            $group=null;
        }else{
            $search_user = [$user_session['id']];
            $group=$user_session['group_id'];
        }
        return ['action_role'=>$action_type_list,'search_user'=>$search_user,'group'=>$group];
    }

    public static function getViewActionRole($user_data){
        $ActionRole=new self();
        $action_role=$ActionRole->alias('ar')
            ->join('action a','ar.action_id=a.id','left')
            ->where('ar.del_flag',0)
            ->where(function ($query)use($user_data){
                $query->where('ar.user_id',$user_data['id']);
                if ($user_data['role']) {
                    $query->whereOr('ar.role_id', $user_data['role']);
                }
                if ($user_data['role_detail_id']) {
                    $query->whereOr('ar.role_detail_id', $user_data['role_detail_id']);
                }
                if($user_data['group_id']){
                    $query->whereOr('ar.group_id',$user_data['group_id']);
                }
            })
            ->where('a.action_type',1)
            ->group('ar.action_id')
            ->column('ar.action_type','a.action_name');

        return $action_role;
    }

    public static function updateActionRole($list,$row,$type,$id)
    {
        $ActionRole=new self();
        $ActionRole->startTrans();
        try {
            $ar=[];
            $arr=[];
            foreach($list as $k=>$v){
                if ($v['check']){
                    if (isset($row[$v['id']])){
                    $arr[$k]=['id'=>$row[$v['id']]['id'],'action_type'=>$v['action_type'],'del_flag'=>0];
//                        $ActionRole->save(['action_type'=>$v['action_type'],'del_flag'=>0],['id'=>$row[$v['id']]['id']]);
                    }else{
                        $ar[$k]=['action_id'=>$v['id'],'action_type'=>$v['action_type']];
                        if ($type === 'user'){
                            $ar[$k]['user_id']=$id;
                        }elseif ($type === 'role'){
                            $ar[$k]['role_id']=$id;
                        }elseif ($type === 'role_detail'){
                            $ar[$k]['role_detail_id']=$id;
                        }elseif ($type === 'group'){
                            $ar[$k]['group_id']=$id;
                        }
                        //Db::name('action_role')->insert(['user_id'=>$user_id,'action_id'=>$v['id'],'action_type'=>$v['action_type']]);
                    }
                }else{
                    if (isset($row[$v['id']])){
//                    $arr[$k][]=['id'=>$row[$v['id']]['id'],'del_flag'=>1];
                        if ($row[$v['id']]['del_flag']==0){
                            $result=$ActionRole->where('id',$row[$v['id']]['id'])->update(['del_flag'=>1]);
                            if ($result===false){
                                return ['status'=>false,'result'=>'修改失败2'];
                            }
                        }
                    }
                }
            }
            if (!empty($arr)){
                $ActionRole->isUpdate(true)->saveAll($arr);
            }
            if (!empty($ar)){
                $ActionRole->isUpdate(false)->saveAll($ar);
            }
            $ActionRole->commit();
            return ['status'=>true,'result'=>'更新成功'];
        }catch (\Exception $e){
            $ActionRole->rollback();
            trace('更新权限失败'.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'更新权限失败'];
        }

    }

}